package com.zjc.gateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.event.EnableBodyCachingEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 网关通用配置
 * 解决取不到body数据问题
 */
@Component
public class GatewayCommonConfig {

    @Autowired
    private GatewayProperties gatewayProperties;

    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void init(){
        gatewayProperties.getRoutes().forEach(e->{
            EnableBodyCachingEvent enableBodyCachingEvent = new EnableBodyCachingEvent(new Object(), e.getId());
            //发布事件
            applicationContext.publishEvent(enableBodyCachingEvent);
        });
    }
}
